/*
 * $Id: HistoryLabelProvider.java 489 2008-08-20 18:23:00Z rcleveng $
 *
 * Copyright (c) 2001,2002 Boris Pruessmann and others. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v0.5 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v05.html
 *
 * Contributors: Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.ui.internal.views;

import java.text.DateFormat;
import java.text.MessageFormat;

import net.sourceforge.perforce.core.api.ILogEntry;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;

/**
 * @version $Revision: 489 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pr??mann</a>
 */
public class HistoryLabelProvider extends LabelProvider implements ITableLabelProvider {
  private static final int COL_REVISION = RevisionsTableCreator.COL_REVISION;
  private static final int COL_FILENAME = RevisionsTableCreator.COL_FILENAME;
  private static final int COL_CHANGELIST = RevisionsTableCreator.COL_CHANGELIST;
  private static final int COL_DATE = RevisionsTableCreator.COL_DATE;
  private static final int COL_USER = RevisionsTableCreator.COL_USER;
  private static final int COL_ACTION = RevisionsTableCreator.COL_ACTION;
  private static final int COL_DESCRIPTION = RevisionsTableCreator.COL_DESCRIPTION;


  /** @see ITableLabelProvider#getColumnImage(Object, int) */
  public Image getColumnImage(Object element, int columnIndex) {
    return null;
  }

  /** @see ITableLabelProvider#getColumnText(Object, int) */
  public String getColumnText(Object element, int columnIndex) {
    ILogEntry entry = (ILogEntry) element;
    switch (columnIndex) {
      case COL_REVISION:
        return entry.getRevision().toString();

      case COL_FILENAME:
        return entry.getDepotFilename();

      case COL_CHANGELIST:
        return MessageFormat.format("{0,number,00000}", //$NON-NLS-1$
            new Object[] {entry.getChangelist()});

      case COL_DATE:
        return DateFormat.getInstance().format(entry.getTime());

      case COL_USER:
        return entry.getUser() + '@' + entry.getClient();

      case COL_ACTION:
        String[] actions = entry.getActions();
        StringBuilder buffer = new StringBuilder();
        for (int i = 0, length = actions.length; i < length; i++) {
          buffer.append(actions[i]);
          if (i < length - 1) {
            buffer.append("; "); //$NON-NLS-1$
          }
        }
        return buffer.toString();

      case COL_DESCRIPTION:
        String description = entry.getDescription();
        int index = description.indexOf('\n');
        switch (index) {
          case 0:
          case -1:
            return description;

          default:
            return description.substring(0, index) + "[...]"; //$NON-NLS-1$
        }
    }

    return ""; //$NON-NLS-1$
  }
}
